﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Garlic.CC.BaseClass;
using Garlic.DM.DataModel;
using Garlic.CC.Utility;
using Garlic.BLL;
using System.Text.RegularExpressions;

public partial class SystemManagement_ModifyPassword : BasePage
{
    

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void GeneralToolBar1_OnClick(object sender, Garlic.UI.Components.IconButtonClickEventArgs args)
    {
        try
        {
            Garlic.CC.BaseClass.IUser user = CurrentUser;

            String regex = @"^[A-Z](?=.*\d).{7,99}$";

            string strMsg = "";
            if (string.IsNullOrEmpty(oldPassword.Text.Trim()))
            {
                strMsg += "请输入旧密码 ";
            }

            if (string.IsNullOrEmpty(newPassword.Text.Trim()))
            {
                strMsg += "请输入新密码 ";
            }

            if (string.IsNullOrEmpty(confrimPassword.Text.Trim()))
            {
                strMsg += "请确认新密码 ";
            }

            if (newPassword.Text.Trim() != confrimPassword.Text.Trim())
            {
                strMsg += "您两次输入的新密码不一致 ";
            }

            if (strMsg != "")
            {
                this.Message = strMsg;
                return;
            }

            //if (user.UserType == UserType.EPS)
            //{
            //    int epsPW;
            //    if (!int.TryParse(newPassword.Text, out epsPW))
            //    {
            //        //输入的密码不是数字
            //        ShowMessage("您输入的新密码格式不正确。密码规则：EPS终端用户的密码必须为数字");
            //        return;
            //    }
            //}
            //else
            //{
                //if (!Regex.IsMatch(oldPassword.Text, regex))
                //{
                //    ShowMessage("您输入的密码格式不正确。密码规则：以大写字母开头，必须包含字母与数字，密码长度不能小于8位。");
                //    return;
                //}
                if (!Regex.IsMatch(newPassword.Text, regex))
                {
                    ShowMessage("您输入的新密码格式不正确。密码规则：以大写字母开头，必须包含字母与数字，密码长度不能小于8位。");
                    return;
                }
            //}

            if (CryptTools.Encrypt(oldPassword.Text, CryptConstants.PassKey) == user.Password)
            {                
                UserManager.ModifyPassword(newPassword.Text, user);
                if (!ClientScript.IsClientScriptBlockRegistered("clearPWDInfo"))
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "clearPWDInfo", "<script language='javascript'>parent.document.getElementById('Header1_lblPwdExpired').innerText='';</script>");
                }
                ShowMessage("密码修改成功。");
            }
            else
            {
                ShowMessage("您输入的旧密码不正确。");
            }
        }
        catch (Exception ex)
        {
            base.HandleException(ex);
        }
    }

    private void ShowMessage(string msg)
    {
        this.Message = msg;
    }
}
